package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.CategoryDao;
import com.pojo.Category;
import com.pojo.Dish;
import com.pojo.Setmeal;
import com.service.CategoryService;
import com.service.DishService;
import com.service.SetmealService;
import com.util.CustomExption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id);
        long count = dishService.count(wrapper);
        if (count>0) {
            throw new CustomExption("当前分类下关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId,id);
        long count1 = setmealService.count(wrapper1);
        if (count1>0) {
            throw new CustomExption("当前分类下关联了套餐，不能删除");
        }
        super.removeById(id);
    }
}
